//
//  LKShopPosterViewController.m
//  Mideleague_Shop
//
//  Created by Zhiwang on 2020/7/10.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKShopPosterViewController.h"

@interface LKShopPosterViewController ()

@property(nonatomic,copy)NSString *merchantName;
@property(nonatomic,copy)NSString *base_url;
@property(nonatomic,copy)NSString *qrcode;
@property(nonatomic,strong)UIImageView *baseImageV;
@property(nonatomic,strong)UIImageView *iconImageV;
@property(nonatomic,strong)UIImageView *codeImageV;
@property(nonatomic,strong)LKBaseLabel *nameLabel;
@end

@implementation LKShopPosterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"商城海报";
    self.baseImageV = [[UIImageView alloc] init];
    [self.view addSubview:self.baseImageV];
    self.baseImageV.image = [UIImage imageNamed:@"ic_storeposters_bg"];
    [self getDetailInformation];
    [self.baseImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view.mas_top).offset(NAVIGATION_BAR_HEIGHT);
        make.height.mas_equalTo(HEIGHT - NAVIGATION_BAR_HEIGHT - 150);
    }];
    self.iconImageV = [[UIImageView alloc] init];
    [self.baseImageV addSubview:self.iconImageV];
    [self.iconImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseImageV.mas_left).offset(20);
        make.top.equalTo(self.baseImageV.mas_top).offset(20);
        make.size.mas_equalTo(CGSizeMake(32, 32));
    }];
    self.iconImageV.layer.cornerRadius = 16;
    self.iconImageV.layer.masksToBounds = YES;
    self.nameLabel = [LKBaseLabel labelWithFrame:CGRectZero text:self.merchantName textColor:[UIColor whiteColor] font:[UIFont fontWithName:@"PingFangSC-Regular" size:15] textAlignment:NSTextAlignmentCenter];
    [self.baseImageV addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImageV.mas_right).offset(10);
        make.centerY.equalTo(self.iconImageV);
    }];
    self.codeImageV = [[UIImageView alloc] init];
    [self.baseImageV addSubview:self.codeImageV];
    [self.codeImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.baseImageV);
        make.size.mas_equalTo(CGSizeMake(164, 164));
    }];
    LKBaseLabel *headerLabel = [LKBaseLabel labelWithFrame:CGRectZero text:@"扫码进入小程序商城" textColor:[UIColor whiteColor] font:[UIFont fontWithName:@"PingFangSC-Regular" size:15] textAlignment:NSTextAlignmentCenter];
    [self.baseImageV addSubview:headerLabel];
    [headerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.codeImageV.mas_bottom).offset(10);
        make.centerX.equalTo(self.baseImageV);
    }];
    WS(weakSelf)
    LKBaseButton *bottomButton = [LKBaseButton buttonWithFrame:CGRectZero type:UIButtonTypeCustom title:@"保存海报到手机相册" titleColor:[UIColor whiteColor] layercorne:15 font:[UIFont fontWithName:@"PingFangSC-Regular" size:14] backgroudColor:yellowDefaultColor AndActionBlock:^(UIButton * _Nonnull btn) {
        // 设置绘制图片的大小
        UIGraphicsBeginImageContextWithOptions(weakSelf.codeImageV.bounds.size, NO, 0.0);
        // 绘制图片
        [weakSelf.codeImageV.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        // 保存图片到相册   如果需要获取保存成功的事件第二和第三个参数需要设置响应对象和方法，该方法为固定格式。
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    }];
    [self.view addSubview:bottomButton];
    [bottomButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(WIDTH/2);
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom).offset(-30);
        make.height.mas_equalTo(30);
    }];
    // Do any additional setup after loading the view.
}
-(void)getDetailInformation{
        [SVProgressHUD showWithStatus:@""];
        NSMutableDictionary *param = [[NSMutableDictionary alloc] initWithDictionary:@{}];
        NSString *sign = [NSString encoingWithDic:param];
        [param setObject:sign forKey:@"sign"];
        [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MyShopCodeUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
            
        } success:^(BOOL isSuccess, id  _Nullable responseObject) {
            [SVProgressHUD dismiss];
            if ([responseObject[@"code"] isEqualToString:@"success"]) {
                self.base_url = responseObject[@"data"][@"base_url"];
                self.merchantName = responseObject[@"data"][@"merchant_name"];
                self.qrcode = responseObject[@"data"][@"qrcode"];
                self.nameLabel.text = self.merchantName;
                [self.iconImageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ServerlUrl,self.base_url]] placeholderImage:[UIImage imageNamed:@""]];
                [self.codeImageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ServerlUrl,self.qrcode]] placeholderImage:[UIImage imageNamed:@""]];
            }else{
                [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
            }
        } failure:^(NSString * _Nullable errorMessage) {
            
        }];
    
}
//图片保存成功调用
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    
    if (error) {
        // 保存失败
        [SVProgressHUD showInfoWithStatus:@"图片保存失败"];
    } else {
        [SVProgressHUD showInfoWithStatus:@"图片保存成功"];
        // 保存成功
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
